package ru.ksu.ktk.diploma.sdi.bean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import ru.ksu.ktk.diploma.sdi.dao.ResourcesDao;
import ru.ksu.ktk.diploma.sdi.dao.model.ResourceModel;
import ru.ksu.ktk.diploma.sdi.model.DescriptionFile;
import ru.ksu.ktk.diploma.sdi.util.FacesUtils;

public class SourceDetailsBean {
    private static final String SOURCES_COLLECTION_NAME = "sources";
    private ResourcesDao resourcesDao;

    public String getSourceName() {

        return FacesUtils.getRequestParameter("chosenSource");
    }

    public String getSourceUri() {
        return FacesUtils.getRequestParameter("chosenSourceUri");
    }

    public List<DescriptionFile> getDescriptionFiles() {
        List<DescriptionFile> files = new ArrayList<DescriptionFile>();
        String sourceUri = getSourceUri();
        // TODO [coldflame]: this check fixes problem with twice method calls
        if (sourceUri != null){
            String sourceCollectionUri = String.format("%s/%s", sourceUri, SOURCES_COLLECTION_NAME);
            List<ResourceModel> resources = resourcesDao.getList(sourceCollectionUri);
            for (ResourceModel resource : resources){
                DescriptionFile descriptionFile = new DescriptionFile(resource.getName());
                files.add(descriptionFile);
            }
        }

        return files;
    }

    public ResourcesDao getResourcesDao() {
        return resourcesDao;
    }

    public void setResourcesDao(ResourcesDao resourcesDao) {
        this.resourcesDao = resourcesDao;
    }

}
